﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace PACKETPARSERMAX20.CORE
{
    public class DefinitionDefault : DefinitionBase
    {
        public DefinitionDefault(DefinitionContext pContext)
            : base(pContext)
        {
        }

        public override bool Process()
        {
             
            if (OpcodeEnum == Opcodes.SMSG_CLIENTCACHE_VERSION)
            {
                ResetPosition();
                var type = ReadUInt32();
            }

            if (OpcodeEnum == Opcodes.SMSG_LEARNED_DANCE_MOVES)
            {
                ResetPosition();
                UInt64 unk = ReadUInt64();
                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_CRITERIA_DELETED)
            {
                ResetPosition();
                UInt32 unk = ReadUInt32();
                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_ALL_ACHIEVEMENT_DATA)
            {
                ResetPosition();

                while (true)
                {
                    Int32 unk1 = ReadInt32();
                    if (unk1 == -1) { break; }
                    base.Stream.Position = base.Stream.Position - 4;
                    UInt32 iter = ReadUInt32();
                    UInt32 secsToTimeBitFields = ReadUInt32();
                }

                while (true)
                {
                    Int32 unk5 = ReadInt32();
                    if (unk5 == -1) { break; }
                    base.Stream.Position = base.Stream.Position - 4;

                    UInt32 iter = ReadUInt32();
                    UInt64 guidcounter = popGuid();
                    UInt64 playerGuid = popGuid();
                    UInt32 unk2 = ReadUInt32();
                    UInt32 secsToTimeBitFields = ReadUInt32();
                    UInt32 unk3 = ReadUInt32();
                    UInt32 unk4 = ReadUInt32();
                }

                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_POWER_UPDATE)
            {
                ResetPosition();
                UInt64 guid = popGuid();
                byte power = ReadByte();
                UInt32 val = ReadUInt32();
                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_AURA_UPDATE_ALL)
            {
                ResetPosition();
                ulong targetguid = popGuid();

                while (AvailableBytes > 0)
                {
                    byte AuraSlot = ReadByte();
                    UInt32 auraId = ReadUInt32();

                    // flags
                    byte auraFlags = ReadByte();

                    // level
                    byte AuraLevel = ReadByte();
                    // charges
                    byte m_procCharges = ReadByte();

                    if (!((auraFlags & (uint)AURA_FLAGS.AFLAG_NOT_CASTER) > 0))
                    {
                        ulong casterguid = popGuid();                   // packed GUID of someone (caster?)
                    }

                    if ((auraFlags & (uint)AURA_FLAGS.AFLAG_DURATION) > 0)          // include aura duration
                    {
                        UInt32 AuraMaxDuration = ReadUInt32();
                        UInt32 AuraDuration = ReadUInt32();
                    }
                }

                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_AURA_UPDATE)
            {
                ResetPosition();
                ulong targetguid = popGuid();
                byte AuraSlot = ReadByte();
                UInt32 auraId = ReadUInt32();

                if (auraId > 0)
                {
                    // flags
                    byte auraFlags = ReadByte();

                    // level
                    byte AuraLevel = ReadByte();
                    // charges
                    byte m_procCharges = ReadByte();

                    //AFLAG_EFF_INDEX_0 True
                    //AFLAG_DURATION True
                    //AFLAG_NEGATIVE True

                    if (!((auraFlags & (uint)AURA_FLAGS.AFLAG_NOT_CASTER) > 0))
                    {
                        UInt32 UInt32_1 = ReadUInt32();
                        UInt32 UInt32_2 = ReadUInt32();
                        UInt32 UInt32_3 = ReadUInt32();
                        UInt32 UInt32_4 = ReadUInt32();
                        UInt32 UInt32_5 = ReadUInt32();
                        ulong casterguid = popGuid();                  // packed GUID of someone (caster?)
                    }

                    if ((auraFlags & (uint)AURA_FLAGS.AFLAG_DURATION) > 0)          // include aura duration
                    {
                        UInt32 AuraMaxDuration = ReadUInt32();
                        UInt32 AuraDuration = ReadUInt32();
                    }
                }

                return Validate();
            }

            if (OpcodeEnum == Opcodes.CMSG_CALENDAR_GET_NUM_PENDING)
            {
                ResetPosition();
                base.Stream.Position = base.Stream.Length;
                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_CALENDAR_SEND_NUM_PENDING)
            {
                ResetPosition();
                UInt32 unk = ReadUInt32();
                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_CRITERIA_UPDATE)
            {
                ResetPosition();
                base.Stream.Position = base.Stream.Length;
                return Validate();
            }




 

            if (OpcodeEnum == Opcodes.SMSG_ACHIEVEMENT_EARNED)
            {
                ResetPosition();
                ulong guid = popGuid();
                UInt32 UInt32_1 = ReadUInt32();
                UInt32 UInt32_2 = ReadUInt32();
                UInt32 UInt32_3 = ReadUInt32();
                return Validate();
            }
            if (OpcodeEnum == Opcodes.SMSG_UPDATE_ACCOUNT_DATA_COMPLETE)
            {
                ResetPosition();

                UInt32 UInt32_1 = ReadUInt32();
                UInt32 UInt32_2 = ReadUInt32();

                return Validate();
            }

            if (OpcodeEnum == Opcodes.CMSG_GAMEOBJ_REPORT_USE)
            {
                ResetPosition();

                ulong guid = ReadUInt64();

                return Validate();
            }

            if (OpcodeEnum == Opcodes.SMSG_PRE_RESURRECT)
            {
                ResetPosition();

                UInt32 result = ReadUInt32();

                return Validate();
            }

            if (Opcode == 1202) //SMSG_UNKNOWN_1202
            {
                ResetPosition();

                Warning = "UNKNOWN";


                return true;



                //Result = string.Format("SMSG_UNKNOWN_1202 - {0}", Core.ObjList[guid].Name);


                //return Validate();
            }

            if (Opcode == 1203) //CMSG_UNKNOWN_1203
            {
                ResetPosition();

                ulong guid = ReadUInt64();

                Warning = string.Format("CMSG_UNKNOWN_1203 - {0}", Core.ObjList[guid].Name);


                return Validate();
            }

            if (Opcode == 1191) //SMSG_UNKNOWN_1191
            {
                ResetPosition();

                ulong guid = popGuid();
                uint unk = ReadUInt32();
                Warning = string.Format("SMSG_UNKNOWN_1191 - {0}", Core.ObjList[guid].Name);


                return Validate();
            }

            if (Opcode == 1192) //CMSG_UNKNOWN_1192
            {
                ResetPosition();

                ulong guid = ReadUInt64();
                Warning = string.Format("CMSG_UNKNOWN_1192 - {0}", Core.ObjList[guid].Name);


                return Validate();
            }

            if (Opcode == 1189) //UMSG_UNKNOWN_1189
            {
                ResetPosition();

                uint unk = ReadUInt32();
                while (AvailableBytes > 0)
                {
                    ulong guid = ReadUInt64();
                    //uint unk2 = ReadUInt32();
                    //uint unk3 = ReadUInt32();
                }

                Warning = string.Format("UMSG_UNKNOWN_1189");

                return Validate();
            }

            if (Opcode == 1271) //UMSG_UNKNOWN_1271
            {
                ResetPosition();

                uint unk = ReadUInt32();

                Warning = string.Format("UMSG_UNKNOWN_1271 unk={0}", unk);

                return Validate();
            }

            if (Opcode == 1271) //UMSG_UNKNOWN_1271
            {
                ResetPosition();

                uint unk = ReadUInt32();

                Warning = string.Format("UMSG_UNKNOWN_1271 unk={0}", unk);

                return Validate();
            }

            if (Opcode == 1208) //UMSG_UNKNOWN_1271
            {
                ResetPosition();

                byte unk = ReadByte();


                return Validate();
            }

            ////if (Opcodes.MAX_OPCODE_ID >= (int)Opcode)
            ////{
            //    //base.ObjPacketLog.Filter.Add(Opcodes., 0);
            //    System.Diagnostics.Debug.WriteLine(((int)Opcode).ToString() + "\t" + Opcode.ToString() + "\t" + AvailableBytes);
            //    context.DefinedOpcodeList.Add(Opcode);

            ////}

            if (Opcode == 1235)   //SMSG_SPLINE_MOVE_UNKNOWN_1235
            {
                ResetPosition();

                var guid = popGuid();


                return Validate();
            }

            if (Opcode == 1236)   //SMSG_SPLINE_MOVE_UNKNOWN_1236
            {
                ResetPosition();

                var guid = popGuid();


                return Validate();
            }

            if (Opcode == 1234)   //SMSG_SPLINE_MOVE_UNKNOWN_1236-LEVITATING
            {
                DEFINITIONS.DefaultMovementInfoDef def = new PACKETPARSERMAX20.CORE.DEFINITIONS.DefaultMovementInfoDef();
                def.Initialize(base.context);
                return def.Process();
            }


            if (OpcodeEnum == Opcodes.CMSG_PLAYER_LOGIN)
            {
                ResetPosition();
                var t = ReadUInt64();
                this.Core.Load_CMSG_PLAYER_LOGIN(t);
                return Validate();

            }
            if (OpcodeEnum == Opcodes.SMSG_LOGIN_VERIFY_WORLD)
            {
                ResetPosition();
                var mapid = ReadUInt32(); //data << pCurrChar->GetMapId();
                var positionX = ReadFloat(); // data << pCurrChar->GetPositionX();
                var positionY = ReadFloat(); //data << pCurrChar->GetPositionY();
                var positionZ = ReadFloat(); //data << pCurrChar->GetPositionZ();
                var positionO = ReadFloat(); // data << pCurrChar->GetOrientation();
                this.Core.Load_SMSG_LOGIN_VERIFY_WORLD(mapid, positionX, positionY, positionZ, positionO);
                return Validate();

            }

            if (OpcodeEnum == Opcodes.SMSG_MOTD)
            {
                ResetPosition();
                var lines = ReadUInt32();
                var sb = new StringBuilder();
                for (int i = 0; i < lines; i++)
                {
                    sb.AppendLine(ReadString());
                }
                return Validate();

            }

            if (OpcodeEnum == Opcodes.SMSG_INITIALIZE_FACTIONS)
            {
                ResetPosition();
                var count = ReadUInt32();
                var player = Core.Player;
                player.FactionInfoList = new SortedList<int, FactionInfo>();

                for (int i = 0; i < count; i++)
                {
                    var flags = ReadByte();
                    var standing = ReadUInt32();

                    player.FactionInfoList.Add(i, new FactionInfo(i, flags, (int)standing));
                }
                return Validate();

            }

            if (OpcodeEnum == Opcodes.SMSG_TUTORIAL_FLAGS ||
            OpcodeEnum == Opcodes.CMSG_REALM_SPLIT ||
            OpcodeEnum == Opcodes.SMSG_REALM_SPLIT ||
            OpcodeEnum == Opcodes.SMSG_CHAR_ENUM ||
            OpcodeEnum == Opcodes.SMSG_FEATURE_SYSTEM_STATUS ||
            OpcodeEnum == Opcodes.SMSG_GUILD_EVENT ||
            OpcodeEnum == Opcodes.SMSG_CONTACT_LIST ||
            OpcodeEnum == Opcodes.SMSG_SET_PROFICIENCY ||
            OpcodeEnum == Opcodes.SMSG_SET_PCT_SPELL_MODIFIER ||
            OpcodeEnum == Opcodes.SMSG_SET_FLAT_SPELL_MODIFIER ||
            OpcodeEnum == Opcodes.SMSG_TALENTS_INFO ||
            OpcodeEnum == Opcodes.SMSG_INSTANCE_DIFFICULTY ||
            OpcodeEnum == Opcodes.CMSG_UPDATE_ACCOUNT_DATA ||
            OpcodeEnum == Opcodes.CMSG_NAME_QUERY ||
            OpcodeEnum == Opcodes.SMSG_INITIAL_SPELLS ||
            OpcodeEnum == Opcodes.SMSG_SEND_UNLEARN_SPELLS ||
            OpcodeEnum == Opcodes.SMSG_INIT_WORLD_STATES ||
            OpcodeEnum == Opcodes.SMSG_MEETINGSTONE_IN_PROGRESS ||
            OpcodeEnum == Opcodes.SMSG_LOGIN_SETTIMESPEED ||
            OpcodeEnum == Opcodes.SMSG_GUILD_BANK_LIST ||
            OpcodeEnum == Opcodes.SMSG_TIME_SYNC_REQ ||
            OpcodeEnum == Opcodes.SMSG_ITEM_ENCHANT_TIME_UPDATE ||
            OpcodeEnum == Opcodes.CMSG_PLAYED_TIME)
            {
                ResetPosition();

                Result = string.Format("has {0} bytes left.", AvailableBytes);
                return true;// Validate();

            }
            return Validate();

        }


    }
}
